/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entidade;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.midlet.MIDlet;
import main.MobCat;

/**
 *
 * @author Victorino
 */
public class AppHelper {

    private MIDlet midlet = null;
    public static AppHelper instance = null;
    public int cidadeSelecionada = -1;
    public String URL = "http://189.92.192.149:8084/MobCatWeb/MobCatService";

    public static AppHelper getInstance() {
        if (instance == null) {
            instance = new AppHelper();
        }
        return instance;
    }

    public MobCat getMidlet() {
        return (MobCat) midlet;
    }

    private AppHelper() {
    }

    public void setMidlet(MIDlet midlet) {
        this.midlet = midlet;
    }

    public void exitMidlet() {
        //RecordStorePool.getInstance().closeAllRecordStore();
        midlet.notifyDestroyed();
    }

    public String resolveIPLocation() {

        if (URL == null) {
            HttpConnection connection = null;
            try {
                connection = (HttpConnection) Connector.open("http://mobcatweb.sytes.net/MobCatWeb/MobCatService");
                connection.setRequestMethod(HttpConnection.GET);
                URL = connection.getHeaderField("location");
                
            } catch (IOException error) {
                System.out.println("Caught IOException: " + error.toString());
            } finally {
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (Exception error) {
                        error.printStackTrace();
                    }
                }
            }
        }
        return URL;
    }
    /*
    public UsuarioConfig getUc() {
    UsuarioConfig uc = MObjectController.getUserConfig();
    if (uc == null) {
    uc = new UsuarioConfig();
    uc.save();
    }
    return uc;
    }

    public void setUc(UsuarioConfig uc) {
    uc.save();
    }*/
}
